package org.ams.common.util;

import org.springframework.stereotype.Component;

import jodd.mail.Email;
import jodd.mail.EmailMessage;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;
import jodd.mail.SmtpSslServer;
import jodd.util.MimeTypes;

@Component
public class EmailUtil {
	
	@SuppressWarnings("rawtypes")
	public boolean sendMail(String toMail, String subject, String text) throws Exception{
		boolean flag = false;
		try {
			Email email = Email.create().from("service_news@163.com").to(toMail)
					.subject(subject).addText(text);
			SmtpServer smtpServer = SmtpServer.create("smtp.163.com")
					.authenticateWith("service_news", "meimima+521-");
			SendMailSession session = smtpServer.createSession();
			session.open();
			session.sendMail(email);
			session.close();
			flag = true;
		} catch (Exception e) {
			throw new RuntimeException("邮件发送失败:"+ e.getMessage());
		}
		return flag;
	}
	public static void main(String[] args) {
		EmailUtil eu = new EmailUtil();
        try {
        	eu.sendQQMail("354540079@qq.com", "您的邮箱", "去死吧");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean sendQQMail(String toMail, String subject, String text) throws Exception{
		boolean flag = false;
		try {
			Email email = Email.create();
			email.addMessage(new EmailMessage("Hello!",
					MimeTypes.MIME_TEXT_PLAIN));
			email.from("354540079@qq.com").to(toMail).subject(subject).addText(text);
			@SuppressWarnings("rawtypes")
			SmtpServer smtpServer = SmtpSslServer.create("smtp.qq.com")
					.authenticateWith("354540079@qq.com", "meimima+521-");
			SendMailSession session = smtpServer.createSession();
			session.open();
			session.sendMail(email);
			session.close();
			flag = true;
		} catch (Exception e) {
			throw new RuntimeException("邮件发送失败:"+ e.getMessage());
		}
		return flag;
	}
}
